package com.uhylab.uhuvinetest.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 项目名称：Uhylab
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/5/31 14:12
 * 修改人：xyb
 * 修改时间：16/5/31 14:12
 * 修改备注：
 */
public class UHUserPregnancyActivity extends UHBaseActivity implements View.OnClickListener, OptionsPickerView.OnOptionsSelectListener, CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.pregnancy_swich)
    Switch mPregnancySwich;
    @BindView(R.id.one)
    AutoRelativeLayout mOne;
    @BindView(R.id.pregnancy_time)
    TextView mPregnancyTime;
    @BindView(R.id.time)
    AutoRelativeLayout mTime;

    /**
     * 怀孕的周数
     */
    private ArrayList<Integer> user_height_Items = new ArrayList<Integer>();
    /**
     * 怀孕的周数的一级联动组件
     */
    private OptionsPickerView mPickerView;


    /**
     * @return 返回布局文件的ID
     */
    @Override
    public int bindLayout() {
        return R.layout.activity_user_pregnancy;
    }

    @Override
    public void initView(View view) {
        ButterKnife.bind(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        mPregnancySwich.setOnCheckedChangeListener(this);
        if (!SharedPreUtils.getBoolean(this, "is_pregancy", false)) {
            mPregnancySwich.setChecked(false);
        } else {
            mPregnancySwich.setChecked(true);
        }

        mPickerView = new OptionsPickerView(this);
        for (int i = 1; i < 50; i++) {
            user_height_Items.add(i);
        }
        mPickerView.setPicker(user_height_Items);
        mPickerView.setTitle("孕期");
        mPickerView.setCyclic(false);
        mPickerView.setLabels("周");
        mPickerView.setOnoptionsSelectListener(this);
        mBack.setOnClickListener(this);
        mTime.setOnClickListener(this);
    }

    @Override
    public void resume() {

        mPregnancyTime.setText(SharedPreUtils.getString(this, "pregancy_time", "0") + "周");

    }

    @Override
    public void destroy() {

    }


    /**
     * 点击事件的监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                Intent intent = new Intent();
                intent.putExtra("date", mPregnancyTime.getText().toString());
                setResult(1, intent);
                finish();
                break;
            case R.id.time:
                if (!mPickerView.isShowing()) {
                    mPickerView.show();
                }
                break;
        }
    }

    /**
     * @param options1 联动的第几个参数
     * @param option2
     * @param options3
     */
    @Override
    public void onOptionsSelect(int options1, int option2, int options3) {
        String time = user_height_Items.get(options1).toString();
        mPregnancyTime.setText(time + "周");
        SharedPreUtils.setString(this, "pregancy_time", time);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        SharedPreUtils.setBoolean(this, "is_pregancy", isChecked);
    }
}
